package com.ruoyi.pingjiao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pingjiao.domain.Evaluation;
import com.ruoyi.pingjiao.domain.EvalutionActivity;
import com.ruoyi.pingjiao.domain.vo.EvaluationActivityVo;
import com.ruoyi.pingjiao.domain.vo.StudentVo;
import com.ruoyi.pingjiao.domain.vo.TeacherWithPaperVo;
import com.ruoyi.pingjiao.service.IEvaluationPaperService;
import com.ruoyi.pingjiao.service.IEvaluationService;
import com.ruoyi.pingjiao.service.IEvalutionActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pingjiao.domain.Student;
import com.ruoyi.pingjiao.service.IStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生管理Controller
 *
 * @author 胡浩鹏
 * @date 2022-10-15
 */
@Api("学生管理")
@RestController
@RequestMapping("/score/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IEvalutionActivityService evalutionActivityService;
    @Autowired
    private IEvaluationPaperService evaluationPaperService;
    @Autowired
    private IEvaluationService evaluationService;

    /**
     * 查询学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 查询学生管理列表不带分页
     */
    @PreAuthorize("@ss.hasPermi('score:student:list')")
    @GetMapping("/listSimple")
    public TableDataInfo listSimple(Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    @ApiOperation("批量导入学生信息，通过Excel文件")
    @PreAuthorize("@ss.hasAnyRoles('collegeAdmin,admin,systemAdmin')")
    @Log(title = "学生管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = studentService.importStudent(studentList, operName);
        return success(message);
    }


    /**
     * 导出学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:student:export')")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生管理数据");
    }
    /**
     * 导出学生管理列表模板
     */
    @PreAuthorize("@ss.hasPermi('score:student:export')")
    @Log(title = "学生列表模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    public void export1(HttpServletResponse response, Student student)
    {
//        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, null, "学生管理数据");
    }
    /**
     * 获取学生管理详细信息
     */
    @ApiOperation("获取学生详细信息")
    @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "long", paramType = "path",
            dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermi('score:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return AjaxResult.success(studentService.selectStudentByStudentId(studentId));
    }
    @ApiOperation("获取学生信息")
    @PreAuthorize("@ss.hasAnyPermi('score:student:query')")
    @GetMapping("/info/{userId}")
    public AjaxResult getStudentInfo(@PathVariable Long userId){
        System.out.println(studentService.getStudentInfo(userId));
        return AjaxResult.success(studentService.getStudentInfo(userId));
    }
    /**
     * 新增学生管理
     */
    @PreAuthorize("@ss.hasPermi('score:student:add')")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        Student s = new Student();
        s.setStudentNo(student.getStudentNo());
        studentService.selectStudentList(s);
        if(!UserConstants.UNIQUE.equals(String.valueOf(studentService.selectStudentList(s).size()))){
            return AjaxResult.error("新增教师'" + student.getRealName() + "'失败，工号"+student.getStudentNo()+"已存在");
        }
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生管理
     */
    @PreAuthorize("@ss.hasPermi('score:student:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生管理
     */
    @PreAuthorize("@ss.hasPermi('score:student:remove')")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(studentService.deleteStudentByStudentIds(studentIds));
    }


    /**
     * @author hpf
     * 获取活动列表
     */
    @ApiOperation("获取活动列表")
    @PreAuthorize("@ss.hasPermi('pingjiao:student:getActivityList')")
    @GetMapping(value = "/getActivityList/{status}")
    public AjaxResult getActivityList(@PathVariable String status)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long studentId = studentService.selectStudentIdByUserId(loginUser.getUserId());
//        Long studentId = 3682L;
        List<EvalutionActivity> studentVo =evalutionActivityService.selectEvalutionActivityByStudentId(studentId,status);
        return AjaxResult.success(studentVo);
    }
    /**
     * @author hpf
     * @param  activityId
     * 获取某活动被评价的教师列表
     */
    @ApiOperation("获取某学生需要评价的课程与教师信息")
    @PreAuthorize("@ss.hasPermi('pingjiao:student:getEvalTeacherList')")
    @GetMapping(value = "/getTeacherList/{activityId}")
    public AjaxResult getEvalTeacherList(@PathVariable  Long activityId)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long studentId = studentService.selectStudentIdByUserId(loginUser.getUserId());
//        Long studentId = 3682L;
        List<EvaluationActivityVo> studentVo =studentService.selectTeacherListByStudentId(studentId,activityId);
        return AjaxResult.success(studentVo);
    }

    /**
     * 获取评价问卷详情
     * @author hpf
     * @param evaluationId
     */
    @ApiOperation("获取评价问卷详情")
    @ApiImplicitParams({@ApiImplicitParam(name= "evaluationId", value = "评价Id", required = true, dataType = "int")

    })
    @PreAuthorize("@ss.hasPermi('pingjiao:student:getPaperForEvalTeacher')")
    @GetMapping(value = "/getPaper/{evaluationId}")
    public AjaxResult getPaperForEvalTeacher(@PathVariable long evaluationId)
    {
        return AjaxResult.success(evaluationPaperService.selectEvaluationPaperOrResultByEvaluationId(evaluationId));
    }
}
